﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Iacym.Entities;

namespace Iacym.Repository
{
    public class SexoRepository : MasterRepository, ISexoRepository
    {
        public List<SEXO> GetAllFromSexo()
        {
            var query = from p in context.SEXOes
                        select p;

            return query.ToList();
        }

        public List<SEXO> GetAllFromSexoByDescripcion(string criterio)
        {
            if (criterio != string.Empty)
            {
                var query = from p in context.SEXOes
                            where p.Descripcion.ToUpper().Contains(criterio.ToUpper())
                            select p;

                return query.ToList();
            }
            else
            {
                var query = from p in context.SEXOes
                            select p;

                return query.ToList();
            }
        }

        public SEXO GetFromSexoById(int id)
        {
            var query = from p in context.SEXOes
                        where p.Id.Equals(id)
                        select p;

            return query.SingleOrDefault();
        }

        public void AddSexo(SEXO sexo)
        {
            context.SEXOes.Add(sexo);
            context.SaveChanges();
        }

        public void UpdateSexo(SEXO sexo)
        {
            context.Entry(sexo).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }

        public void DeleteSexo(int id)
        {
            var existe = context.SEXOes.Where(p => p.Id.Equals(id)).SingleOrDefault();

            if (existe != null)
            {
                context.SEXOes.Remove(existe);
                context.SaveChanges();
            }
        }

        public bool ExisteDuplicidad(string descripcion, int id)
        {
            var query = from p in context.SEXOes
                        where p.Descripcion.ToUpper().CompareTo(descripcion.ToUpper()) == 0
                        && p.Id != id
                        select p;

            return query.Any();
        }
    }
}
